// pages/goodslist/goodslist.js
Page({
  data: {
    sortType: "all",
    sortPrice: 1,
    goodsName: "",
    page: 1,
    goodsList: [],
    arrange: 1,
  },
  onLoad(query) {
    this.setData({ goodsName: query.goodsName });
    this.getGoodsList();
  },
  changeAll() {
    this.setData({ sortType: "all" });
    this.getGoodsList();
  },
  changeSales() {
    this.setData({ sortType: "sales" });
    this.getGoodsList();
  },
  changePrice() {
    this.setData({ sortType: "price" });
    if (this.data.sortType === "price") {
      if (this.data.sortPrice === 0) {
        this.setData({ sortPrice: 1 });
      } else {
        this.setData({ sortPrice: 0 });
      }
    }
    this.getGoodsList();
  },
  async getGoodsList() {
    const res = await wx.http.get("/goods/list", {
      sortType: this.data.sortType,
      sortPrice: this.data.sortPrice,
      goodsName: this.data.goodsName,
      page: this.data.page,
    });
    const goodsList = res.data.data.list.data;
    this.setData({ goodsList });
  },
  changeSort() {
    if (this.data.arrange === 1) {
      this.setData({ arrange: 2 });
    } else {
      this.setData({ arrange: 1 });
    }
  },
  inputChange(ev) {
    this.setData({
      goodsName: ev.detail.value,
    });
  },
  toDetail(ev) {
    wx.navigateTo({
      url: `../gooddetail/gooddetail?goods_id=${ev.mark.goods_id}`
    })
  }
});
